<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">员工通知</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toNoticeAdd(0)">{{
        translate("add")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('title')"
        prop="title"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span class="han1" :title="scope.row.title">{{
            scope.row.title
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('content')"
        prop="content"
        min-width="160"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span class="han1" :title="scope.row.content">{{
            scope.row.content
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('tzyg')"
        prop="recipientsNum"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span class="tzry" :title="scope.row.sysUserName">{{
            scope.row.recipientsNum
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('fbry')"
        prop="publishName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('pubTime')"
        prop="publishTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('status')"
        min-width="80"
        fixed="right"
      >
        <template slot-scope="scope">
          {{ scope.row.status == "0" ? translate("dfb") : translate("yfb") }}
        </template>
      </el-table-column>
      <el-table-column
        :label="translate('handle')"
        min-width="120"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="scope.row.status == '0'"
              v-preventReClick
              type="text"
              @click="toNoticePublish(scope.row.id)"
              >{{ translate("fb") }}</el-button
            >
            <el-button
              v-if="scope.row.status == '0'"
              v-preventReClick
              type="text"
              @click="toNoticeAdd('1', scope.row.id)"
              >{{ translate("edit") }}</el-button
            >
            <el-button
              v-preventReClick
              type="text"
              style="color: red"
              @click="toNoticeDelete(scope.row.id)"
              >{{ translate("del") }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="cur_page"
        :hide-on-single-page="true"
        @current-change="getList"
      />
    </div> -->
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <!-- 新增 编辑 消息 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      :title="addEditType ? translate('messageEdit') : translate('messageAdd')"
      :visible.sync="showAddNotice"
      @closed="cancelAddNotice"
    >
      <el-form
        ref="ruleForm"
        :model="formAddNotice"
        :rules="rules"
        label-width="auto"
      >
        <el-form-item :label="translate('title')" prop="title">
          <el-input v-model="formAddNotice.title" />
        </el-form-item>
        <el-form-item :label="translate('content')" prop="content">
          <el-input v-model="formAddNotice.content" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item :label="translate('tzyg')" prop="recipientsIds">
          <el-select
            v-model="formAddNotice.recipientsIds"
            value-key="id"
            :placeholder="translate('chooseRole')"
            multiple
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in recipientsList"
              :key="item.id"
              :label="item.userName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancelAddNotice">{{
          translate("cancel")
        }}</el-button>
        <el-button
          v-preventReClick
          type="success"
          @click="confirmAddNotice('0')"
          >{{ translate("save") }}</el-button
        >
        <el-button
          v-preventReClick
          :loading="publishLoading"
          type="primary"
          @click="confirmAddNotice('1')"
          >{{ translate("fb") }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { accountList, noticeList, noticeAdd, noticePublish, noticeEdit, noticeParam, noticeDelete } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeSystem',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      publishLoading: false,
      listLoading: false,
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {
        type: '1'
      },
      addEditType: 0,
      showAddNotice: false,
      formAddNotice: {
        title: '',
        content: '',
        recipientsIds: [],
        type: '1'
      },
      recipientsList: [],
      rules: {
        title: [
          { required: true, message: this.translate('titlePlace'), trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.translate('contentPlace'), trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        recipientsIds: [
          { type: 'array', required: true, message: this.translate('tzygPlace'), trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList(1)
    this.getAccountList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      noticeList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = { type: '1' }
      this.getList(1)
    },
    // 获取账号列表
    getAccountList() {
      accountList({}).then(res => {
        if (res.errorCode === '000000') {
          this.recipientsList = res.recordList
        }
      })
    },
    // 新增/编辑消息
    toNoticeAdd(type, id) {
      this.addEditType = type
      this.showAddNotice = true
      if (id) {
        noticeParam({ param: { id } }).then(res => {
          if (res.errorCode === '000000') {
            res.record.recipientsIds = res.record.recipientsList
            res.record.type = '1'
            this.formAddNotice = res.record
          }
        })
      }
    },
    // 取消新增/编辑
    cancelAddNotice() {
      this.showAddNotice = false
      this.formAddNotice = {
        title: '',
        content: '',
        recipientsIds: [],
        type: '1'
      }
    },
    // 确认新增/编辑
    confirmAddNotice(isPublish) {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...that.formAddNotice }
          param.isPublish = isPublish
          that.publishLoading = true
          if (addEditType) {
            noticeEdit({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.showAddNotice = false
              that.getList(1)
            })
          } else {
            noticeAdd({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.showAddNotice = false
              that.getList(1)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 发布通知
    toNoticePublish(id) {
      noticePublish({ param: { id } }).then(res => {
        if (res.errorCode !== '000000') return
        this.getList(1)
      })
    },
    // 删除通知
    toNoticeDelete(id) {
      this.$confirm(this.translate('noticeDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          noticeDelete({ param: { id } }).then(res => {
            this.getList(1)
          })
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        })
    }
  }
}
</script>
<style scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tzry {
  cursor: pointer;
  display: inline-block;
  width: 100%;
  color: #435ebe;
}
</style>
